<template>
  <scroll class="listview" :data="data" ref="listview" :listenScroll="listenScroll" @scroll="scroll"
          :probeType="probeType">
    <ul>
      <li v-for="group in data" class="listgroup" ref="listgroup">
        <h2 class="list-group-titlt">{{group.title}}</h2>
        <ul>
          <li v-for="item in group.items" class="list-group-item" @click="selectItem(item)">
            <img v-lazy="item.avatar" class="avatar">
            <span class="name">{{item.name}}</span>
          </li>
        </ul>
      </li>
    </ul>
    <div class="liste-shortcut" @touchstart="onShortcutTouchStart" @touchmove.stop.prevent="onShortcutTouchMove">
      <ul>
        <li v-for="(item, index) in shortcutList" class="item" :data-index="index"
            :class="{'current': currentIndex===index}">{{item}}
        </li>
      </ul>
    </div>
    <div class="list-fix" v-show="fixedTitle, data.length" ref="fixed">
      <h1 class="fixed-title">{{fixedTitle}}</h1>
    </div>
    <div class="loading-content" v-show="!data.length">
      <loading></loading>
    </div>
  </scroll>
</template>

<script type="text/ecmascript-6">
  import Scroll from 'base/scroll/scroll'
  import {getData} from 'common/js/dom.js'
  import Loading from 'base/load/load'
  const ANCHOR_HEIGHT = 20
  const TITLE_HEIGHT = 30
  export default {
    props: {
      data: {
        type: Array,
        default: []
      }
    },
    data() {
      return {
        scrollY: -1,
        currentIndex: 0,
        diff: -1
      }
    },
    created() {
      this.touch = {}
      this.listenScroll = true
      this.listHeight = []
      this.probeType = 3
    },
    computed: {
      shortcutList() {
        return this.data.map((group) => {
          return group.title.substr(0, 1)
        })
      },
      fixedTitle() {
          if (this.scrollY > 0) {
            return ''
          }
          return this.data[this.currentIndex] ? this.data[this.currentIndex].title : ' '
      }
    },
    methods: {
      selectItem(item) {
        this.$emit('select', item)
      },
      onShortcutTouchStart(e) {
        let anchorIndex = getData(e.target, 'index')
        let firstTouch = e.touches[0]
        this.touch.y1 = firstTouch.pageY
        this.touch.anchorIndex = anchorIndex
        this.$refs.listview.scrollToElement(this.$refs.listgroup[anchorIndex], 0)
        this._scrollTo(anchorIndex)
      },
      onShortcutTouchMove(e) {
        let firstTouch = e.touches[0]
        this.touch.y2 = firstTouch.pageY
        let delta = (this.touch.y2 - this.touch.y1) / ANCHOR_HEIGHT | 0
        let anchorIndex = parseInt(this.touch.anchorIndex) + delta
        this._scrollTo(anchorIndex)
      },
      refreshFN() {
          this.$refs.listview.refresh()
      },
      scroll(pos) {
        this.scrollY = pos.y
      },
      _scrollTo(index) {
          if (index === null) {
              return
          }
          if (index < 0) {
              index = 0
          } else if (index > this.listHeight.length - 2) {
              index = this.listHeight.length - 2
          }
          this.scrollY = -this.listHeight[index]
        this.$refs.listview.scrollToElement(this.$refs.listgroup[index], 0)
      },
      _calculateHeight() {
        this.listHeight = []
        const list = this.$refs.listgroup
        let height = 0
        this.listHeight.push(height)
        for (let i = 0; i < list.length; i++) {
          let item = list[i]
          height += item.clientHeight
          this.listHeight.push(height)
        }
      }
    },
    watch: {
      data() {
        setTimeout(() => {
          this._calculateHeight()
        }, 20)
      },
    scrollY(newY) {
        const listHeight = this.listHeight
//         当滚动到底部   newY>0
      if (newY > 0) {
            this.currentIndex = 0
        return
      }
//      在中间滚动
         for (let i = 0; i < listHeight.length - 1; i++) {
          let height1 = listHeight[i]
          let height2 = listHeight[i + 1]
          if (!height2 || (-newY >= height1 && -newY < height2)) {
            this.currentIndex = i
            this.diff = height2 + newY
            return
          }
        }
//        滚动到底部，并且-newY大于最后一个元素的上限
      this.currentIndex = listHeight.length - 2
      },
      diff(newVal) {
          let fixedTop = (newVal > 0 && newVal < TITLE_HEIGHT) ? newVal - TITLE_HEIGHT : 0
        if (this.fixedTop === fixedTop) {
              return
        }
        this.fixedTop = fixedTop
        this.$refs.fixed.style.transform = `translate3d(0, ${fixedTop}px, 0)`
      }
    },
    components: {
      Scroll,
      Loading
    }
  }
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
  @import "~common/stylus/variable"

  .listview
    position: fixed
    width: 100%
    top: 88px
    bottom: 0
    z-index: -10
    .listgroup
      height: 100%
      h2
        padding: 10px 0
        width: 100%
        margin-bottom:10px
        text-indent: 20px
        font-size $font-size-small
        background-color: $color-highlight-background
      .list-group-item
        margin: 15px 0
        margin-left 20px
        display flex
        .avatar
          border-radius 50%
          width: 50px
          height: 50px
          flex 0 0 50px
        .name
          display: flex
          flex-direction column
          text-indent 20px
          font-size $font-size-small
          justify-content center

  .liste-shortcut
    position: absolute
    width: 20px
    height: 100%;
    top: 80px
    right: 0
    ul
      width: 100%
      height: 100%
      li
        height: 20px
        font-size $font-size-small
        text-align center
        line-height: 20px;
        &.current
          color: $color-theme
.list-fix
  position: fixed
  top:88px
  width: 100%
  .fixed-title
      padding: 10px 0
      width: 100%
      margin-bottom:10px
      text-indent: 20px
      font-size $font-size-small
      background-color: $color-highlight-background

</style>
